package com.pet.serve.controller;

import com.pet.serve.entity.Customers;
import com.pet.serve.entity.Goods;
import com.pet.serve.entity.Invest;
import com.pet.serve.entity.Pets;
import com.pet.serve.service.CustomerService;
import com.pet.serve.service.ExportService;
import com.pet.serve.service.GoodsService;
import com.pet.serve.service.InvestService;
import com.pet.serve.service.PetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ExportController {

    private final ExportService exportService;
    private final CustomerService customerService;
    private final GoodsService goodsService;
    private final PetsService petsService;
    private final InvestService investService;

    @Autowired
    public ExportController(ExportService exportService, CustomerService customerService, GoodsService goodsService, PetsService petsService, InvestService investService) {
        this.exportService = exportService;
        this.customerService = customerService;
        this.goodsService = goodsService;
        this.petsService = petsService;
        this.investService = investService;
    }

    @PostMapping("/user/export")
    public ResponseEntity<byte[]> exportCustomers() {
        try {
            List<Customers> customers = customerService.getAllCustomers();
            byte[] fileContent = exportService.exportCustomers(customers);
            return buildResponse(fileContent, "customers.xlsx");
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/goods/export")
    public ResponseEntity<byte[]> exportGoods() {
        try {
            List<Goods> goods = goodsService.getAllGoods();
            byte[] fileContent = exportService.exportGoods(goods);
            return buildResponse(fileContent, "goods.xlsx");
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/pet/export")
    public ResponseEntity<byte[]> exportPets() {
        try {
            List<Pets> pets = petsService.getAllPets();
            byte[] fileContent = exportService.exportPets(pets);
            return buildResponse(fileContent, "pets.xlsx");
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/invest/export")
    public ResponseEntity<byte[]> exportInvests() {
        try {
            List<Invest> invests = investService.getAllInvests();
            byte[] fileContent = exportService.exportInvests(invests);
            return buildResponse(fileContent, "invests.xlsx");
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private ResponseEntity<byte[]> buildResponse(byte[] fileContent, String filename) {
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename);
        headers.set(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel");
        return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
    }
}
